package controllers

import (
	"github.com/gin-gonic/gin"
	"social-app/config"
	"social-app/pkg/response"
)

type FileController struct{}

type fileResponse struct {
	Url string `json:"url"`
}

func (f FileController) UploadImage(c *gin.Context) {
	file, err := c.FormFile("image")
	if err != nil {
		panic(err.Error())
	}
	if file.Size == 0 {
		panic("上传文件无效")
	}
	allowExt := CheckUploadImageExt(file, config.AppConfig.UploadImageExt)
	if allowExt == false {
		panic("上传文件非法")
	}
	saveImageName := GetUploadFilePath(file.Filename, "images")
	err = c.SaveUploadedFile(file, saveImageName)
	response.Success(c, "上传成功", &fileResponse{
		Url: saveImageName,
	})
}

func (f FileController) UploadVideo(c *gin.Context) {
	file, err := c.FormFile("video")
	if err != nil {
		panic(err.Error())
	}
	if file.Size == 0 {
		panic("上传文件无效")
	}
	allowExt := CheckUploadImageExt(file, config.AppConfig.UploadVideoExt)
	if allowExt == false {
		panic("上传文件非法")
	}
	saveImageName := GetUploadFilePath(file.Filename, "images")
	err = c.SaveUploadedFile(file, saveImageName)
	response.Success(c, "上传成功", &fileResponse{
		Url: saveImageName,
	})
}

func (f FileController) UploadMusic(c *gin.Context) {
	file, err := c.FormFile("music")
	if err != nil {
		panic(err.Error())
	}
	if file.Size == 0 {
		panic("上传文件无效")
	}
	allowExt := CheckUploadImageExt(file, config.AppConfig.UploadMusicExt)
	if allowExt == false {
		panic("上传文件非法")
	}
	saveImageName := GetUploadFilePath(file.Filename, "images")
	err = c.SaveUploadedFile(file, saveImageName)
	response.Success(c, "上传成功", &fileResponse{
		Url: saveImageName,
	})
}
